#include"TcpSocket.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include"TcpServer.h"
#include"SecKeyShm.h"
#include"ShareMemory.h"
#include"xsec.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
    // 1. 创建通信的套接字
    TcpSocket tcp;

    // 2. 连接服务器IP port
    int ret = tcp.connectToHost("127.0.0.1", 10000);
    if (ret == -1)
    {
        return -1;
    }
    
    //从共享内存中读取密钥
    char clientid[8]={0};
    char serverid[8]={0};
    strcpy(clientid,"1002");
    strcpy(serverid,"1001");
    SecKeyShm *shm=new SecKeyShm(0x0018);
  	NodeSHMInfo node;
  	memset(&node,0x00,sizeof(NodeSHMInfo));
  	shm->shmRead(clientid,serverid,&node);

  	string passwd=string(node.seckey,strlen(node.seckey));
    //准备加密数据
    XSec sec;
    ret=sec.XSecEncryptFile(X3DES_CBC, passwd, "./english.txt", "./cipher.txt", true);
    sec.Close();
    if(!ret)
    {
    	cout<<"encrpto failed!"<<endl;
    	return -1;
    }
    // 3. 通信
    int fd1 = open("./cipher.txt", O_RDONLY);

    int length = 0;
    char tmp[128];
    memset(tmp, 0, sizeof(tmp));
    while ((length = read(fd1, tmp, sizeof(tmp))) > 0)
    {
        // 发送数据
        int sendsize=tcp.sendMsg(string(tmp,length),clientid,serverid);
        memset(tmp, 0, sizeof(tmp));

        // 接收数据
        usleep(300);
    }

    sleep(10);

    return 0;
}